package com.ychs.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ychs.bean.Clazz;
import com.ychs.service.IClazzService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 描述【教室的操作】
 * Copyright: Copyright (c) 2018
 * Company: 英才汇硕信息技术有限公司
 *
 * @author tianchuanjie
 * @version 1.0
 * @created 2018/12/21 15:32
 */
@Controller
public class ClazzController {
    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IClazzService clazzService;

    /**
     * 访问教室jsp页面
     * @return
     */
    @GetMapping("/clazz")
    public String clazzInfo(){
        return "/admin/clazzInfo";
    }

    @GetMapping("/clazzInfo")
    public void  ClazzInfo(int page, int limit, HttpServletResponse resp){

        logger.debug(page);
        logger.debug(limit);

        resp.setCharacterEncoding("UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();
        //查询所有教室的信息
        List<Clazz> clazzlist = clazzService.queryListPage(limit*(page-1),limit);
        Map<String ,Object> map = new HashMap<>();
        //分页查询layui的参数
        map.put("msg","");
        map.put("code",0);

        int clazzCount = clazzService.queryCount();
        map.put("count",clazzCount);
        map.put("data",clazzlist);
        PrintWriter out = null;

        try {
            out= resp.getWriter();
            out.print(objectMapper.writeValueAsString(map));
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.flush();
        out.close();
    }


    /**
     * 新增教室信息
     * @param clazz 教室的信息
     * @param response
     * @return
     */
    @PostMapping("/addClazz")
    public String addClazzInfo(Clazz clazz,HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        //新增教室信息
        boolean result = clazzService.insert(clazz);
        return "/admin/clazzInfo";
    }

    /**
     * 删除教室信息
     * @param clazz 教室对象信息
     * @return
     */
    @GetMapping("/deleteClazz")
    public String deleteClazzInfo(Clazz clazz){
        //删除教室信息
        boolean result = clazzService.delete(clazz);
        return "/admin/clazzInfo";
    }

    /**
     * 更新教室信息
     * @param clazz 教室对象信息
     * @return
     */
    @PostMapping("/updateClazz")
    public String updateClazzInfo(Clazz clazz){
        boolean result = clazzService.update(clazz);
        return "/admin/clazzInfo";
    }
}
